package com.bugs.reptile;

import com.model.JobInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 猎聘html解释器
 */
public class LiepinParser implements Parser {
    private final String BASE_URL = "https://www.liepin.com/";
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy年MM月dd日");

    @Override
    public boolean hasNextPage(Html html) {
        return nextPageUrl(html, null) != null;
    }

    @Override
    public String nextPageUrl(Html html, String url) {
        Elements aTips = Jsoup.parse(html.$("div.pagerbar").get()).getElementsByTag("a");
        String link = aTips.get(4).attr("href");
        if(link.equals("javascript:;")) return null;
        return BASE_URL + link;
    }

    @Override
    public List<String> getDetailUrls(Html html) {
        // 过滤掉 猎头 信息
        List<Selectable> lis = html.$("div.sojob-result ul li").nodes();
        lis = lis.stream().filter(selectable -> {
            Selectable i = selectable.$("i.icon-yellow-triangle");
            return i.get() != null;
        }).collect(Collectors.toList());
        List<String> detailUrls = new ArrayList<>(lis.size());
        lis.forEach(li -> {
            Document linkDoc = Jsoup.parse(li.$("div.job-info a").get());
            detailUrls.add(linkDoc.getElementsByTag("a").attr("href"));
        });
        return detailUrls;
    }

    @Override
    public JobInfo convertJobInfo(Html html) {
        JobInfo jobInfo = new JobInfo();
        // 标题
        jobInfo.setRecruitTitle(Jsoup.parse(html.$("div.title-info").get()).getElementsByTag("h1").text());
        // 月薪
        Document jobItem = Jsoup.parse(html.$("div.job-item").get());
        String wage = jobItem.getElementsByClass("job-item-title").text().split(" ")[0];
        jobInfo.setWage(wage);
        float[] wages = convertWage(jobInfo.getWage());
        if(wages != null) {
            jobInfo.setMinWage(wages[0]);
            jobInfo.setMaxWage(wages[1]);
        }
        // 学历, 年限要求 如: 本科及以上 3年以上 语言不限 年龄不限
        String[] claimArr = jobItem.getElementsByClass("job-qualifications").get(0).text().split(" ");
        jobInfo.setAcademicRequirements(claimArr[0]);
        jobInfo.setExpRange(claimArr[1]);
        // 发布日期
        String dateStr = jobItem.getElementsByTag("time").attr("title");
        jobInfo.setReleaseDate(this.convertDate(dateStr));
        // 福利标签
        jobInfo.setWelfareTip(jobItem.getElementsByClass("comp-tag-list").text());
        // 公司名称, 行业, 规模, 地址
        Document companyItem = Jsoup.parse(html.$("div.right-post-top").get());
        jobInfo.setCompanyName(companyItem.getElementsByClass("normalFace").attr("alt"));
        Elements companyInfos = companyItem.getElementsByClass("new-compintro");
        companyInfos = Jsoup.parse(companyInfos.tagName("li").html()).getElementsByTag("li");
        jobInfo.setIndustry(trim(trim(companyInfos.get(0).text()).split("：")[1]));
        jobInfo.setScale(trim(trim(companyInfos.get(1).text()).split("：")[1]));
        jobInfo.setWorkAddress(trim(trim(companyInfos.get(2).text()).split("：")[1]));
        return jobInfo;
    }

    // 月薪转换
    private float[] convertWage(String wage) {
        if(wage.equals("面议")) return null;
        // 格式 25-38k·14薪
        String[] wageArr = wage.split("·");
        String[] payStrArr = wageArr[0].split("-");
        Float payCount = Float.parseFloat(wageArr[1].substring(0, wageArr[1].length() - 1));
        float scale = payCount / 12;
        float minWage = Float.parseFloat(payStrArr[0]) * 1000 * scale;
        float maxWage = Float.parseFloat(payStrArr[1].replace("k", "")) * 1000 * scale;
        float[] wages = new float[2];
        wages[0] = minWage;
        wages[1] = maxWage;
        return wages;
    }

    private LocalDate convertDate(String str) {
        return LocalDate.parse(str, DATE_TIME_FORMATTER);
    }
}




























